package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.entity.UserRole;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class RoleServiceImpl implements RoleService {

    private final RoleMapper roleMapper;

    private final RoleResourceMapper roleResourceMapper;

    private final UserRoleMapper userRoleMapper;

    /**
     * 分页查询角色列表
     * @param roleDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<RoleVo> page(RoleDto roleDto, Integer pageNum, Integer pageSize) {
        //完成分页
        PageHelper.startPage(pageNum, pageSize);
        //查询数据
        Page<RoleVo> pageInfo = roleMapper.pageInfo(roleDto);
        //返回数据
        return PageResponse.of(pageInfo,RoleVo.class);
    }

    /**
     * 添加角色
     * @param roleDto
     */
    @Override
    public void addRole(RoleDto roleDto) {
        //拷贝属性
        Role role = BeanUtil.copyProperties(roleDto, Role.class);
        //插入数据
        role.setDataState(SuperConstant.DATA_STATE_0);
        roleMapper.insertSelective(role);
    }

    /**
     * 查询角色已选资源
     * @param roleId
     * @return
     */
    @Override
    public List<String> findCheckedResources(Long roleId) {
        return roleResourceMapper.selectRoleByRoleId(roleId);
    }

    /**
     * 修改角色
     * @param roleDto
     */
    @Transactional
    @Override
    public Boolean updateRole(RoleDto roleDto) {
        //拷贝属性
        Role role = BeanUtil.copyProperties(roleDto, Role.class);
        //角色是否关联用户
        UserRole userRole = userRoleMapper.selectByRoleId(roleDto.getId());
        if (userRole != null && userRole.getUserId() != null){
            throw new BaseException(BasicEnum.ROLE_DISTRIBUTED);
        }

        int count = roleMapper.updateByPrimaryKeySelective(role);

        String[] checkedResourceNos = roleDto.getCheckedResourceNos();
        if(checkedResourceNos != null && checkedResourceNos.length > 0){
            //删除角色已选资源
            roleResourceMapper.deleteByRoleId(roleDto.getId());
            //新增角色已选资源
            List<RoleResource> newRoleResource = Arrays.stream(checkedResourceNos).map(no ->
                    RoleResource.builder().roleId(roleDto.getId())
                            .resourceNo(no)
                            .dataState(SuperConstant.DATA_STATE_0)
                            .build()).collect(Collectors.toList());
            roleResourceMapper.batchInsert(newRoleResource);
        }
        return count > 0;
    }


    /**
     * 删除角色
     * @param roleId
     */
    @Transactional
    @Override
    public void deleteRole(Long roleId) {
        // 是否被用户关联
        UserRole userRole = userRoleMapper.selectByRoleId(roleId);
        if (userRole != null && userRole.getUserId() != null){
            throw new BaseException(BasicEnum.ROLE_DISTRIBUTED);
        }
        //是否启用
        Role role = roleMapper.selectByPrimaryKey(roleId);
        if (SuperConstant.DATA_STATE_0.equals(role.getDataState())){
            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }
        //删除
        roleMapper.deleteByPrimaryKey(roleId);
        //删除关联资源
        roleResourceMapper.deleteByRoleId(roleId);
    }

    /**
     * 查询所有角色
     */
    @Override
    public List<RoleVo> getAll() {
        return roleMapper.selectAll();
    }
}
